package com.runachina.java;

import android.text.TextUtils;
import android.util.Log;

import com.runachina.common.BaseActivity;

import java.util.Arrays;

/**
 * Created by Isay on 2016/2/2.
 */
public class SendDataAnalysis {

    private static int baowen_l = 0;//报文长度
    private static int sendbyte_l = 0;//发送字节长度

    /*完整发送字节：*/
    public static byte[] sendByte = {0x00};
    /*转发协议*/
    public static byte[] transmit = {
            0x68,
            (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,//长度00CA，是实际长度*4+2，实际长度从4B开始到校验和前
            0x68, 0x4B,
            (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x02, 0x10, (byte) 0x71, 0x00, 0x00, 0x01, 0x00,
            0x00,//端口，0x02:485，0x03:232，0x04:mbus
            0x6B, (byte) 0x83, 0x05,
            0x00, 0x00,//转发的报文的长度
    };
    /*读取电量*/
    public static byte[] readPwr = {0x68, 0x32, 0x00, 0x32, 0x00, 0x68, 0x4b,
            (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
            0x02, 0x0C, (byte) 0x71, 0x00, 0x00, 0x01, 0x00, (byte) 0xC7, 0x16};


    /**
     * 注：瑞纳热表和瑞纳阀门读取数据是一样的 只需要修改类型和指令
     * <p>
     * 1拼接出sendByte：转发协议+报文
     * 2sendByte：设置串口
     * 3sendByte：设置两个长度
     * 4sendByte：设置抄表方式
     * <p>
     * baoWen：报文，如抄表报文
     * meterNum：表号，输入的表号
     * position：表号起始位置
     * baowenCheckPosition:报文校验和起始位置
     */

//     /*读取runa 188协议的热表*/
//    public static byte[] readmeter = {(byte) 0xfe, (byte) 0xfe, (byte) 0xfe, 0x68, 0x20,
//            (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, (byte) 0xAA,//表号
//            (byte) 0xAA, (byte) 0xAA, (byte) 0xAA, 0x01, 0x03, (byte) 0x1F, (byte) 0x90, 0x00,
//            (byte) 0x00, 0x16,
//            (byte) 0x00, 0x16//存放转发协议的校验和结束
//    };
    public static byte[] ReadMeterSendByte(byte[] baoWen, String meterNum, int numPosition, int baowenCheckPosition) {

        /*清空sendbyte,这个很有必要*/
        sendByte = null;
        try {
            /*没有输入表号则为0xAA*/
            if (meterNum.length() >= 8) {

                baoWen[numPosition + 6] = (byte) (0x11);
                baoWen[numPosition + 5] = (byte) (0x11);
                baoWen[numPosition + 4] = (byte) (0x00);
                baoWen[numPosition + 3] = (byte) ((meterNum.charAt(0) - 0x30) * 16 + (meterNum.charAt(1) - 0x30));
                baoWen[numPosition + 2] = (byte) ((meterNum.charAt(2) - 0x30) * 16 + (meterNum.charAt(3) - 0x30));
                baoWen[numPosition + 1] = (byte) ((meterNum.charAt(4) - 0x30) * 16 + (meterNum.charAt(5) - 0x30));
                baoWen[numPosition] = (byte) ((meterNum.charAt(6) - 0x30) * 16 + (meterNum.charAt(7) - 0x30));
            } else {
                baoWen[numPosition + 6] = (byte) (0xAA);
                baoWen[numPosition + 5] = (byte) (0xAA);
                baoWen[numPosition + 4] = (byte) (0xAA);
                baoWen[numPosition + 3] = (byte) (0xAA);
                baoWen[numPosition + 2] = (byte) (0xAA);
                baoWen[numPosition + 1] = (byte) (0xAA);
                baoWen[numPosition] = (byte) (0xAA);
            }

			/*1设置报文校验和：倒数第4个字节是报文的校验和*/
            baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, baowenCheckPosition, 4);

            baowen_l = baoWen.length;
            //拼接 转发协议+报文
            sendByte = concat(transmit, baoWen);
            sendbyte_l = sendByte.length;

            //2设置：两个长度，以及抄表方式
            sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

            //3设置：调试工具的串口     //后来需要补上

            //4设置校验和
            sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        } catch (Exception e) {
            //DisplayToast("表号错误 !");
        }
        return sendByte;
    }


    /**
     * 读取亿林表
     *
     * @param baoWen
     * @param meterNum
     * @param numPosition
     * @param baowenCheckPosition
     * @return
     */
    public static byte[] yiLinReadMeterSendByte(byte[] baoWen, String meterNum, int numPosition, int baowenCheckPosition) {

        /*清空sendbyte,这个很有必要*/
        sendByte = null;
        if (!TextUtils.isEmpty(meterNum)) {
            if (meterNum.length()==4){
                //字符串十六进制转化为十进制 再转回十六进制
                int h = (Integer.parseInt(meterNum.substring(0, 1), 16)) * 16+(Integer.parseInt(meterNum.substring(1, 2), 16));
                int i = (Integer.parseInt(meterNum.substring(2, 3), 16)) * 16+(Integer.parseInt(meterNum.substring(3, 4), 16));

                baoWen[numPosition + 1] = (byte) h;
                baoWen[numPosition] = (byte) i;
            }

        } else {
            baoWen[numPosition + 1] = (byte) (0xAA);
            baoWen[numPosition] = (byte) (0xAA);
        }
        try {
            /*1设置报文校验和：倒数第3个字节是报文的校验和*/
            baoWen[(baoWen.length - 3)] = yilinCalculationCheck(baoWen, baowenCheckPosition, 3);

            baowen_l = baoWen.length;
            //拼接 转发协议+报文
            sendByte = concat(transmit, baoWen);
            sendbyte_l = sendByte.length;

            //2设置：两个长度，以及抄表方式
            sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

            //3设置：调试工具的串口     //后来需要补上

            //4设置校验和
            sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        } catch (Exception e) {
            //DisplayToast("表号错误 !");
        }
        return sendByte;
    }

    /**
     * 公共：拼接两个字节数组：byte1 + byte2
     */
    public static <T> byte[] concat(byte[] byte1, byte[] byte2) {
        byte[] result = Arrays.copyOf(byte1, byte1.length + byte2.length);
        System.arraycopy(byte2, 0, result, byte1.length, byte2.length);
        return result;
    }

    /**
     * 公共：计算校验和w
     */
    private static byte calculationCheck(byte[] aByte, int startP, int stopP) {
        // aByte[(aByte.length - stopP)] = 0x00;
        int check = 0;
        for (int i = startP; i < (aByte.length - stopP); i++) {
            check = check + aByte[i];
        }
        Log.e("liyuelong", "calculationCheck: " + check);
        return (byte) check;
    }

    /**
     * 公共：计算校验和w
     * 亿林阀门计算校验和
     */
    private static byte yilinCalculationCheck(byte[] aByte, int startP, int stopP) {
        // aByte[(aByte.length - stopP)] = 0x00;
        int check = 0;
        for (int i = startP; i < (aByte.length - stopP); i++) {
            check = check + aByte[i];
        }
        check = check & 0xFF ^ 0xA5;
        Log.e("liyuelong", "calculationCheck: " + check);
        return (byte) check;
    }

    /**
     * 公共：计算发送byte中的两个长度，以及抄表方式
     */
    private static byte[] cal_Length_Method(byte[] sendByte, int baowen_l, int sendbyte_l) {
        //两个长度
        if (sendbyte_l > 8) {
            sendByte[1] = (byte) (((sendbyte_l - 8) * 4 + 2) >> 0);//（从4B到校验之前）*4+2, 4B之前4个 + 校验 +0x16
            sendByte[2] = (byte) (((sendbyte_l - 8) * 4 + 2) >> 8);
            sendByte[3] = (byte) (((sendbyte_l - 8) * 4 + 2) >> 0);
            sendByte[4] = (byte) (((sendbyte_l - 8) * 4 + 2) >> 8);
            //转发的报文的长度
            //<<  :左移运算符，num << 1,相当于num乘以2
            //>>  : 右移运算符，num >> 1,相当于num除以2
            sendByte[22] = (byte) ((baowen_l - 2) >> 0);//-2是减去校验和以及0x16
            sendByte[23] = (byte) ((baowen_l - 2) >> 8);
        }
        //抄表方式
        if (BaseActivity.readMethod.equals(BaseActivity.READMETHOD[0]))
            sendByte[18] = 0x04;
        else if (BaseActivity.readMethod.equals(BaseActivity.READMETHOD[1]))
            sendByte[18] = 0x03;
        else {
            sendByte[18] = 0x02;
        }
        return sendByte;
    }


    /**
     * 修改表号：sendDataAnalysis
     */
    public static byte[] modifyNum(byte[] baoWen, String oldnumStr, String newnumStr) {
        //以下是获取修改表号的报文
        sendByte = null;
        String nowData = BaseActivity.getData();
        baoWen[32] = (byte) ((nowData.charAt(0) - 0x30) * 16 + (nowData.charAt(1) - 0x30));
        baoWen[31] = (byte) ((nowData.charAt(2) - 0x30) * 16 + (nowData.charAt(3) - 0x30));
        baoWen[30] = (byte) ((nowData.charAt(4) - 0x30) * 16 + (nowData.charAt(5) - 0x30));
        baoWen[29] = (byte) ((nowData.charAt(6) - 0x30) * 16 + (nowData.charAt(7) - 0x30));
        String nowTime = BaseActivity.getTime();
        baoWen[28] = (byte) ((nowTime.charAt(0) - 0x30) * 16 + (nowData.charAt(1) - 0x30));
        Log.e("lyl", "modifyNum: " + nowTime);
        Log.e("lyl", "modifyNum: " + nowTime.charAt(0));
        Log.e("lyl", "modifyNum: " + String.valueOf(nowTime.charAt(0) - 0x30));
        Log.e("lyl", "modifyNum: " + String.valueOf((nowTime.charAt(0) - 0x30) * 16));
        baoWen[27] = (byte) ((nowTime.charAt(2) - 0x30) * 16 + (nowTime.charAt(3) - 0x30));
        baoWen[26] = (byte) ((nowTime.charAt(4) - 0x30) * 16 + (nowTime.charAt(5) - 0x30));

        baoWen[10] = (byte) ((oldnumStr.charAt(0) - 0x30) * 16 + (oldnumStr.charAt(1) - 0x30));

        baoWen[9] = (byte) ((oldnumStr.charAt(2) - 0x30) * 16 + (oldnumStr.charAt(3) - 0x30));
        baoWen[8] = (byte) ((oldnumStr.charAt(4) - 0x30) * 16 + (oldnumStr.charAt(5) - 0x30));
        baoWen[7] = (byte) ((oldnumStr.charAt(6) - 0x30) * 16 + (oldnumStr.charAt(7) - 0x30));

        baoWen[22] = (byte) ((newnumStr.charAt(0) - 0x30) * 16 + (newnumStr.charAt(1) - 0x30));
        baoWen[21] = (byte) ((newnumStr.charAt(2) - 0x30) * 16 + (newnumStr.charAt(3) - 0x30));
        baoWen[20] = (byte) ((newnumStr.charAt(4) - 0x30) * 16 + (newnumStr.charAt(5) - 0x30));
        baoWen[19] = (byte) ((newnumStr.charAt(6) - 0x30) * 16 + (newnumStr.charAt(7) - 0x30));


        /*1设置报文校验和：倒数第4个字节是报文的校验和*/
        baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, 5, 4);

        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;

        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

        //3设置：调试工具的串口 //后来需要补上

        //4设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        return sendByte;
    }


    /**
     * 修改时间：sendDataAnalysis
     */
    public static byte[] modifyTime(byte[] baoWen, String timeStr) {
        sendByte = null;
        String nowData = BaseActivity.getData();
        String nowtime = BaseActivity.getTime();
        if (timeStr.length() == 14) {
            nowData = timeStr.substring(0, 8);
            nowtime = timeStr.substring(8, 14);
        }
        baoWen[25] = (byte) ((nowData.charAt(0) - 0x30) * 16 + (nowData.charAt(1) - 0x30));
        baoWen[24] = (byte) ((nowData.charAt(2) - 0x30) * 16 + (nowData.charAt(3) - 0x30));
        baoWen[23] = (byte) ((nowData.charAt(4) - 0x30) * 16 + (nowData.charAt(5) - 0x30));
        baoWen[22] = (byte) ((nowData.charAt(6) - 0x30) * 16 + (nowData.charAt(7) - 0x30));

        baoWen[21] = (byte) ((nowtime.charAt(0) - 0x30) * 16 + (nowtime.charAt(1) - 0x30));
        baoWen[20] = (byte) ((nowtime.charAt(2) - 0x30) * 16 + (nowtime.charAt(3) - 0x30));
        baoWen[19] = (byte) ((nowtime.charAt(4) - 0x30) * 16 + (nowtime.charAt(5) - 0x30));

        /*1设置报文校验和：倒数第4个字节是报文的校验和*/
        baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, 5, 4);

        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;

        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

        //3设置：调试工具的串口 //后来需要补上

        //4设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        return sendByte;
    }

    /**
     * 设置属性
     */
    public static byte[] setAttr(byte[] baoWen) {
        sendByte = null;
        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;

        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

        //3设置：调试工具的串口 //后来需要补上

        //4设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);
        return sendByte;
    }

    /**
     * 参数设置--设置参数
     */
    public static byte[] setDataByte(byte[] baoWen) {

        /*1设置报文校验和：倒数第4个字节是报文的校验和*/
        baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, 5, 4);

        sendByte = null;
        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;

        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);
        //3设置：调试工具的串口 //后来需要补上

        //4设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        return sendByte;
    }

    /**
     * 流量修正-读取
     */
    public static byte[] flowRead(byte[] baoWen, String meterNum) {

        try {
            /*没有输入表号则为0xAA*/
            if (meterNum.length() == 8) {
                baoWen[11] = (byte) (0x00);
                baoWen[10] = (byte) ((meterNum.charAt(0) - 0x30) * 16 + (meterNum.charAt(1) - 0x30));
                baoWen[9] = (byte) ((meterNum.charAt(2) - 0x30) * 16 + (meterNum.charAt(3) - 0x30));
                baoWen[8] = (byte) ((meterNum.charAt(4) - 0x30) * 16 + (meterNum.charAt(5) - 0x30));
                baoWen[7] = (byte) ((meterNum.charAt(6) - 0x30) * 16 + (meterNum.charAt(7) - 0x30));
            } else {
                baoWen[11] = (byte) (0xAA);
                baoWen[10] = (byte) (0xAA);
                baoWen[9] = (byte) (0xAA);
                baoWen[8] = (byte) (0xAA);
                baoWen[7] = (byte) (0xAA);
            }
        } catch (Exception e) {
        }
        ;

        /*1设置报文校验和：倒数第4个字节是报文的校验和*/
        baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, 5, 4);

        sendByte = null;
        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;

        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);
        //3设置：调试工具的串口 //后来需要补上

        //4设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        return sendByte;
    }

    /**
     * 流量修正-设置
     */
    public static byte[] flowSet(byte[] baoWen, String meterNum, int manyPoint, double v1, double v2, double v3, double v4, double v5) {

        try {
            /*没有输入表号则为0xAA*/
            if (meterNum.length() == 8) {
                baoWen[13] = (byte) (0x11);
                baoWen[12] = (byte) (0x11);
                baoWen[11] = (byte) (0x00);
                baoWen[10] = (byte) ((meterNum.charAt(0) - 0x30) * 16 + (meterNum.charAt(1) - 0x30));
                baoWen[9] = (byte) ((meterNum.charAt(2) - 0x30) * 16 + (meterNum.charAt(3) - 0x30));
                baoWen[8] = (byte) ((meterNum.charAt(4) - 0x30) * 16 + (meterNum.charAt(5) - 0x30));
                baoWen[7] = (byte) ((meterNum.charAt(6) - 0x30) * 16 + (meterNum.charAt(7) - 0x30));
            } else {
                baoWen[13] = (byte) (0xAA);
                baoWen[12] = (byte) (0xAA);
                baoWen[11] = (byte) (0xAA);
                baoWen[10] = (byte) (0xAA);
                baoWen[9] = (byte) (0xAA);
                baoWen[8] = (byte) (0xAA);
                baoWen[7] = (byte) (0xAA);
            }
        } catch (Exception e) {
        }
        ;

        //设置流量值
        int err1 = (int) ((1.0 / (1 + v1 / 100.0)) * 4096.0);
        int err2 = (int) ((1.0 / (1 + v2 / 100.0)) * 4096.0);
        int err3 = (int) ((1.0 / (1 + v3 / 100.0)) * 4096.0);
        int err4 = (int) ((1.0 / (1 + v4 / 100.0)) * 4096.0);


        baoWen[21] = (byte) (err1 >> 8);
        baoWen[20] = (byte) (err1 >> 0);

        baoWen[23] = (byte) (err2 >> 8);
        baoWen[22] = (byte) (err2 >> 0);

        baoWen[25] = (byte) (err3 >> 8);
        baoWen[24] = (byte) (err3 >> 0);

        baoWen[27] = (byte) (err4 >> 8);
        baoWen[26] = (byte) (err4 >> 0);

        if (manyPoint == 5) {
            int err5 = (int) ((1.0 / (1 + v5 / 100.0)) * 4096.0);
            baoWen[29] = (byte) (err5 >> 8);
            baoWen[28] = (byte) (err5 >> 0);
        }
        /*1设置报文校验和：倒数第4个字节是报文的校验和*/
        baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, 5, 4);

        sendByte = null;
        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;

        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);
        //3设置：调试工具的串口 //后来需要补上

        //4设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        return sendByte;
    }

    /**
     * 检定状态
     */
    public static byte[] inCheck(byte[] baoWen, String meterNum) {
        try {
            /*没有输入表号则为0xAA*/
            if (meterNum.length() == 8) {
                baoWen[13] = (byte) (0x11);
                baoWen[12] = (byte) (0x11);
                baoWen[11] = (byte) (0x00);
                baoWen[10] = (byte) ((meterNum.charAt(0) - 0x30) * 16 + (meterNum.charAt(1) - 0x30));
                baoWen[9] = (byte) ((meterNum.charAt(2) - 0x30) * 16 + (meterNum.charAt(3) - 0x30));
                baoWen[8] = (byte) ((meterNum.charAt(4) - 0x30) * 16 + (meterNum.charAt(5) - 0x30));
                baoWen[7] = (byte) ((meterNum.charAt(6) - 0x30) * 16 + (meterNum.charAt(7) - 0x30));
            } else {
                baoWen[13] = (byte) (0xAA);
                baoWen[12] = (byte) (0xAA);
                baoWen[11] = (byte) (0xAA);
                baoWen[10] = (byte) (0xAA);
                baoWen[9] = (byte) (0xAA);
                baoWen[8] = (byte) (0xAA);
                baoWen[7] = (byte) (0xAA);
            }
        } catch (Exception e) {
        }
        ;

        /*1设置报文校验和：倒数第4个字节是报文的校验和*/
        baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, 5, 4);

        sendByte = null;
        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;

        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);
        //3设置：调试工具的串口 //后来需要补上

        //4设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        return sendByte;
    }

    /**
     * runa modbus协议的表：读表
     */
    public static byte[] runamodbus(byte[] baoWen) {


        sendByte = null;
        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;

        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);


        //4设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        return sendByte;
    }

    /**
     * otherCompany协议的表：读表
     */
    public static byte[] otherCompanyRead(byte[] baoWen, String meterNum, String flags, int startP, int stopP) {

        try {
            if ((meterNum.length() != 0) && (flags.equals("over?yes"))) {
                for (int i = 0, j = 0; i < (stopP - startP + 1); i++) {
                    baoWen[stopP - i] = (byte) ((meterNum.charAt(j++) - 0x30) * 16 + (meterNum.charAt(j++) - 0x30));
                }
            }
            if ((meterNum.length() != 0) && (flags.equals("over?no"))) {
                for (int i = 0, j = 0; i < (stopP - startP + 1); i++) {
                    baoWen[startP + i] = (byte) ((meterNum.charAt(j++) - 0x30) * 16 + (meterNum.charAt(j++) - 0x30));
                }
            }
        } catch (Exception e) {

        }
        ;

        /*1设置报文校验和：倒数第4个字节是报文的校验和*/
        baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, 3, 4);

        sendByte = null;
        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;

        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

        //3设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        return sendByte;
    }

    /**
     * 欧标协议的表：读表
     */
    public static byte[] europeRead(byte[] baoWen, String meterNum, String flags, int startP, int stopP) {

        try {
            if ((meterNum.length() != 0) && (flags.equals("over?yes"))) {
                for (int i = 0, j = 0; i < (stopP - startP + 1); i++) {
                    baoWen[stopP - i] = (byte) ((meterNum.charAt(j++) - 0x30) * 16 + (meterNum.charAt(j++) - 0x30));
                }
            }
            if ((meterNum.length() != 0) && (flags.equals("over?no"))) {
                for (int i = 0, j = 0; i < (stopP - startP + 1); i++) {
                    baoWen[startP + i] = (byte) ((meterNum.charAt(j++) - 0x30) * 16 + (meterNum.charAt(j++) - 0x30));
                }
            }
        } catch (Exception e) {

        }
        ;

        /*1设置报文校验和：倒数第4个字节是报文的校验和*/
        baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, 7, 4);

        sendByte = null;
        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;

        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

        //3设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        return sendByte;
    }

    /**
     * 汇中协议的表：读表
     */
    public static byte[] huizhongRead(byte[] baoWen, String meterNum, String flags, int startP, int stopP) {

        try {
            for (int i = 0, j = 0; i < (stopP - startP + 1); i++) {
                if (i == 0)
                    baoWen[startP + i] = (byte) (0xF0 + (meterNum.charAt(j++) - 0x30));
                else
                    baoWen[startP + i] = (byte) ((meterNum.charAt(j++) - 0x30) * 16 + (meterNum.charAt(j++) - 0x30));
            }
        } catch (Exception e) {
        }
        ;

        /*1设置报文校验和：倒数第4个字节是报文的校验和*/
        baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, 3, 4);

        sendByte = null;
        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;

        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

        //3设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        return sendByte;
    }

    /**
     * 读取电量
     */
    public static byte[] readPwr(byte[] baoWen) {

        sendByte = null;
        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;
        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

        //3设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        return sendByte;
    }

    /**
     * 公用：设置两个长度，抄表方式，完整的校验和。
     * 适用：报文已经校验过的
     *
     * @param baoWen
     * @return
     */
    public static byte[] contactTransmit(byte[] baoWen) {

        sendByte = null;
        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;
        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

        //3设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        return sendByte;
    }

    /*****************************************************瑞纳阀门*************************************************************/


    /**
     * 恢复出厂：sendDataAnalysis
     */
    public static byte[] factoryReset(byte[] baoWen, String meterNum, int numPosition, int baowenCheckPosition) {

        /*清空sendbyte,这个很有必要*/
        sendByte = null;
        try {
            /*没有输入表号则为0xAA*/
            if (meterNum.length() >= 8) {

                baoWen[numPosition + 6] = (byte) (0x11);
                baoWen[numPosition + 5] = (byte) (0x11);
                baoWen[numPosition + 4] = (byte) (0x00);
                baoWen[numPosition + 3] = (byte) ((meterNum.charAt(0) - 0x30) * 16 + (meterNum.charAt(1) - 0x30));
                baoWen[numPosition + 2] = (byte) ((meterNum.charAt(2) - 0x30) * 16 + (meterNum.charAt(3) - 0x30));
                baoWen[numPosition + 1] = (byte) ((meterNum.charAt(4) - 0x30) * 16 + (meterNum.charAt(5) - 0x30));
                baoWen[numPosition] = (byte) ((meterNum.charAt(6) - 0x30) * 16 + (meterNum.charAt(7) - 0x30));
            } else {
                baoWen[numPosition + 6] = (byte) (0xAA);
                baoWen[numPosition + 5] = (byte) (0xAA);
                baoWen[numPosition + 4] = (byte) (0xAA);
                baoWen[numPosition + 3] = (byte) (0xAA);
                baoWen[numPosition + 2] = (byte) (0xAA);
                baoWen[numPosition + 1] = (byte) (0xAA);
                baoWen[numPosition] = (byte) (0xAA);
            }

			/*1设置报文校验和：倒数第4个字节是报文的校验和*/
            baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, baowenCheckPosition, 4);

            baowen_l = baoWen.length;
            //拼接 转发协议+报文
            sendByte = concat(transmit, baoWen);
            sendbyte_l = sendByte.length;

            //2设置：两个长度，以及抄表方式
            sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

            //3设置：调试工具的串口     //后来需要补上

            //4设置校验和
            sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        } catch (Exception e) {
            //DisplayToast("表号错误 !");
        }
        return sendByte;
    }


    /**
     * 数据清零：sendDataAnalysis
     */
    public static byte[] dataReset(byte[] baoWen, String meterNum, int numPosition, int baowenCheckPosition) {

        /*清空sendbyte,这个很有必要*/
        sendByte = null;
        try {
            /*没有输入表号则为0xAA*/
            if (meterNum.length() >= 8) {

                baoWen[numPosition + 6] = (byte) (0x11);
                baoWen[numPosition + 5] = (byte) (0x11);
                baoWen[numPosition + 4] = (byte) (0x00);
                baoWen[numPosition + 3] = (byte) ((meterNum.charAt(0) - 0x30) * 16 + (meterNum.charAt(1) - 0x30));
                baoWen[numPosition + 2] = (byte) ((meterNum.charAt(2) - 0x30) * 16 + (meterNum.charAt(3) - 0x30));
                baoWen[numPosition + 1] = (byte) ((meterNum.charAt(4) - 0x30) * 16 + (meterNum.charAt(5) - 0x30));
                baoWen[numPosition] = (byte) ((meterNum.charAt(6) - 0x30) * 16 + (meterNum.charAt(7) - 0x30));
            } else {
                baoWen[numPosition + 6] = (byte) (0xAA);
                baoWen[numPosition + 5] = (byte) (0xAA);
                baoWen[numPosition + 4] = (byte) (0xAA);
                baoWen[numPosition + 3] = (byte) (0xAA);
                baoWen[numPosition + 2] = (byte) (0xAA);
                baoWen[numPosition + 1] = (byte) (0xAA);
                baoWen[numPosition] = (byte) (0xAA);
            }

			/*1设置报文校验和：倒数第4个字节是报文的校验和*/
            baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, baowenCheckPosition, 4);

            baowen_l = baoWen.length;
            //拼接 转发协议+报文
            sendByte = concat(transmit, baoWen);
            sendbyte_l = sendByte.length;

            //2设置：两个长度，以及抄表方式
            sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

            //3设置：调试工具的串口     //后来需要补上

            //4设置校验和
            sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        } catch (Exception e) {
            //DisplayToast("表号错误 !");
        }
        return sendByte;
    }

    /**
     * 修改表号：sendDataAnalysis
     */
    public static byte[] valveModifyNum(byte[] baoWen, String oldnumStr, String newnumStr) {
        //以下是获取修改表号的报文
        sendByte = null;

        //原表号
        baoWen[10] = (byte) ((oldnumStr.charAt(0) - 0x30) * 16 + (oldnumStr.charAt(1) - 0x30));
        baoWen[9] = (byte) ((oldnumStr.charAt(2) - 0x30) * 16 + (oldnumStr.charAt(3) - 0x30));
        baoWen[8] = (byte) ((oldnumStr.charAt(4) - 0x30) * 16 + (oldnumStr.charAt(5) - 0x30));
        baoWen[7] = (byte) ((oldnumStr.charAt(6) - 0x30) * 16 + (oldnumStr.charAt(7) - 0x30));

        //新表号
        baoWen[22] = (byte) ((newnumStr.charAt(0) - 0x30) * 16 + (newnumStr.charAt(1) - 0x30));
        baoWen[21] = (byte) ((newnumStr.charAt(2) - 0x30) * 16 + (newnumStr.charAt(3) - 0x30));
        baoWen[20] = (byte) ((newnumStr.charAt(4) - 0x30) * 16 + (newnumStr.charAt(5) - 0x30));
        baoWen[19] = (byte) ((newnumStr.charAt(6) - 0x30) * 16 + (newnumStr.charAt(7) - 0x30));

        /*1设置报文校验和：倒数第4个字节是报文的校验和*/
        baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, 5, 4);

        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;

        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

        //3设置：调试工具的串口 //后来需要补上

        //4设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        return sendByte;
    }
/*****************************************************瑞纳温度采集器***********************************************************/

    /**
     reset：sendDataAnalysis
     */
    public static byte[] temDataReset(byte[] baoWen, String meterNum, int numPosition, int baowenCheckPosition) {

        /*清空sendbyte,这个很有必要*/
        sendByte = null;
        try {
            /*没有输入表号则为0xAA*/
            if (meterNum.length() >= 8) {

                baoWen[numPosition + 6] = (byte) (0x11);
                baoWen[numPosition + 5] = (byte) (0x11);
                baoWen[numPosition + 4] = (byte) (0x01);
                baoWen[numPosition + 3] = (byte) ((meterNum.charAt(0) - 0x30) * 16 + (meterNum.charAt(1) - 0x30));
                baoWen[numPosition + 2] = (byte) ((meterNum.charAt(2) - 0x30) * 16 + (meterNum.charAt(3) - 0x30));
                baoWen[numPosition + 1] = (byte) ((meterNum.charAt(4) - 0x30) * 16 + (meterNum.charAt(5) - 0x30));
                baoWen[numPosition] = (byte) ((meterNum.charAt(6) - 0x30) * 16 + (meterNum.charAt(7) - 0x30));
            } else {
                baoWen[numPosition + 6] = (byte) (0xAA);
                baoWen[numPosition + 5] = (byte) (0xAA);
                baoWen[numPosition + 4] = (byte) (0xAA);
                baoWen[numPosition + 3] = (byte) (0xAA);
                baoWen[numPosition + 2] = (byte) (0xAA);
                baoWen[numPosition + 1] = (byte) (0xAA);
                baoWen[numPosition] = (byte) (0xAA);
            }

			/*1设置报文校验和：倒数第4个字节是报文的校验和*/
            baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, baowenCheckPosition, 4);

            baowen_l = baoWen.length;
            //拼接 转发协议+报文
            sendByte = concat(transmit, baoWen);
            sendbyte_l = sendByte.length;

            //2设置：两个长度，以及抄表方式
            sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

            //3设置：调试工具的串口     //后来需要补上

            //4设置校验和
            sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        } catch (Exception e) {
            //DisplayToast("表号错误 !");
        }
        return sendByte;
    }


    /**
     * 温度采集器  发送的数据体需要进行余三码编码
     * 修改表号：sendDataAnalysis
     */
    public static byte[] temModifyNum(byte[] baoWen, String oldnumStr, String newnumStr) {
        //以下是获取修改表号的报文
        sendByte = null;

        //原表号
        baoWen[10] = (byte) ((oldnumStr.charAt(0) - 0x30) * 16 + (oldnumStr.charAt(1) - 0x30));
        baoWen[9] = (byte) ((oldnumStr.charAt(2) - 0x30) * 16 + (oldnumStr.charAt(3) - 0x30));
        baoWen[8] = (byte) ((oldnumStr.charAt(4) - 0x30) * 16 + (oldnumStr.charAt(5) - 0x30));
        baoWen[7] = (byte) ((oldnumStr.charAt(6) - 0x30) * 16 + (oldnumStr.charAt(7) - 0x30));

        //新表号
        baoWen[22] = (byte) (((newnumStr.charAt(0) - 0x30) * 16 + (newnumStr.charAt(1) - 0x30))+0x33);
        baoWen[21] = (byte) (((newnumStr.charAt(2) - 0x30) * 16 + (newnumStr.charAt(3) - 0x30))+0x33);
        baoWen[20] = (byte) (((newnumStr.charAt(4) - 0x30) * 16 + (newnumStr.charAt(5) - 0x30))+0x33);
        baoWen[19] = (byte) (((newnumStr.charAt(6) - 0x30) * 16 + (newnumStr.charAt(7) - 0x30))+0x33);

        /*1设置报文校验和：倒数第4个字节是报文的校验和*/
        baoWen[(baoWen.length - 4)] = calculationCheck(baoWen, 5, 4);

        baowen_l = baoWen.length;
        sendByte = concat(transmit, baoWen);
        sendbyte_l = sendByte.length;

        //2设置：两个长度，以及抄表方式
        sendByte = cal_Length_Method(sendByte, baowen_l, sendbyte_l);

        //3设置：调试工具的串口 //后来需要补上

        //4设置校验和
        sendByte[(sendByte.length - 2)] = calculationCheck(sendByte, 6, 2);

        return sendByte;
    }
}
